﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jemer.Domain;
using Jemer.Data;
using Jemer.Data.Infrastructure;
using Jemer.Domain.Entities;
using Jemer.Data.Repositories;

namespace Jemer.Service
{
    
    public interface IProductService
    {
        IQueryable<Product> GetProducts();
        Product GetProduct(int id);
        void CreateProduct(Product product);
        void DeleteProduct(int id);
        void SaveProduct();
    }
    
    public class ProductService : IProductService
    {
        private readonly IProductRepository productRepository;
        private readonly IUnitOfWork unitOfWork;
        public ProductService(IProductRepository productRepository, IUnitOfWork unitOfWork)
        {
            this.productRepository = productRepository;
            this.unitOfWork = unitOfWork;
        }  
        #region IProductService Members

        public IQueryable<Product> GetProducts()
        {
            var products = productRepository.GetAll();
            return products;
        }

        public Product GetProduct(int id)
        {
            var product = productRepository.GetById(id);
            return product;
        }

        public void CreateProduct(Product product)
        {
            productRepository.Add(product);
            SaveProduct();
        }

        public void DeleteProduct(int id)
        {
            var product = productRepository.GetById(id);
            productRepository.Delete(product);
            SaveProduct();
        }

        public void SaveProduct()
        {
            unitOfWork.Commit();
        }

        #endregion
    }
    
    
}